<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="模拟上传multipart/form-data/脚本生成时间轴"/>




  <meta name="keywords" content="multipart/form-data, 八一" />



  <meta name="baidu-site-verification" content="HhUstaSjr0" />



  <meta name="google-site-verification" content="UA-102975942-1" />






  <link rel="alternate" href="/atom.xml" title="八一">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="https://bay1.top/2018/05/11/模拟上传multipart-form-data-脚本生成时间轴/"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />
<link rel="stylesheet" type="text/css" href="/css/prettify.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/sons-of-obsidian.css" media="screen" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  
  <script id="baidu_analytics">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?9a885cc9fb6cd7bcef579deb8efe8a70";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>



  <script id="google_analytics">
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-102975942-1', 'auto');
        ga('send', 'pageview');
  </script>










    <title> 模拟上传multipart/form-data/脚本生成时间轴 - 八一 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">八一</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/archives">
        <li class="mobile-menu-item">
          
          
            文章
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            关于/友链
          
        </li>
      </a>
    
      <a href="/search">
        <li class="mobile-menu-item">
          
          
            站内搜索
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">八一</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives">
            
            
              文章
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              关于/友链
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/search">
            
            
              站内搜索
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          模拟上传multipart/form-data/脚本生成时间轴
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2018-05-11
        </span>
        
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#首先是时间轴的脚本"><span class="toc-text">首先是时间轴的脚本</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#PPT批量上传的脚本"><span class="toc-text">PPT批量上传的脚本</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <p>模拟上传multipart/form-data,脚本生成时间轴<a id="more"></a></p>
<h2 id="首先是时间轴的脚本"><a href="#首先是时间轴的脚本" class="headerlink" title="首先是时间轴的脚本"></a>首先是时间轴的脚本</h2><p><a href="https://atcumt.com/history.html" target="_blank" rel="noopener">最终效果</a><br>首先前端代码是找的模板,懒省事但是历史线文字是需要我们来填补的吧<br>工作室历史线比较长，也不可能手动来搞～～～～</p>
<blockquote>
<p>分下源代码，我们就可以整理下思路,我们现在要做的就是读取整理好的历史文本里的内容<br>然后生成html内容,因为这个模板每个历史展现是相同的,只是item代号不同,也只是分为两种<br>一开始为了方便，我直接生成html文本,然后手动加到了静态文件history.html里面<br>(运营整理好的是word,对代码的读取不太友好，我就生成了txt文本,然后手动整理成下面的格式</p>
</blockquote>
<p><img src="https://s1.ax1x.com/2018/05/11/C0tRe0.png" alt="QQ图片20180509001033.png"><br>)</p>
<p>很显然，可以直接用python进行O/I操作，这里你会遇到一些转码的错误<br>搜索一下应该就能解决了(至于搜索花费的时间看脸。。。。。。)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">i = <span class="number">0</span></span><br><span class="line">result =<span class="string">''</span></span><br><span class="line"><span class="keyword">with</span> open(<span class="string">'data/timeline.txt'</span>,<span class="string">'r'</span>, encoding=<span class="string">'UTF-8'</span>) <span class="keyword">as</span> file:</span><br><span class="line">    lines = file.readlines()</span><br><span class="line"><span class="keyword">while</span> i &lt; len(lines):</span><br><span class="line">    result += <span class="string">'&lt;div class="timeline__item timeline__item--'</span>+str(int(i/<span class="number">2</span>)+<span class="number">1</span>)+<span class="string">' timeline__item-bg"&gt;'</span>+<span class="string">'\n'</span>+<span class="string">'&lt;div class="timeline__item__station"&gt;&lt;/div&gt;'</span>+<span class="string">'\n'</span>+<span class="string">'&lt;div class="timeline__item__content"&gt;'</span> + \</span><br><span class="line">                <span class="string">'\n'</span>+<span class="string">'&lt;h2 class="timeline__item__content__date"&gt;'</span> + \</span><br><span class="line">                lines[i]+<span class="string">"&lt;/h2&gt;"</span>+<span class="string">'\n'</span>+<span class="string">'&lt;p class="timeline__item__content__description"&gt;'</span> + \</span><br><span class="line">                lines[i+<span class="number">1</span>]+<span class="string">'&lt;/p&gt;'</span>+<span class="string">'\n'</span>+<span class="string">"&lt;/div&gt;"</span>+<span class="string">'\n'</span>+<span class="string">"&lt;/div&gt;"</span></span><br><span class="line">    i += <span class="number">2</span></span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这里解释一下~<br>我们的流程是读出数据，然后得出总的数据长度进行遍历<br>再用字符串的形式串接html代码,其中timeline_item指定颜色,由于我们一条历史分为两行<br>我们需要timeline_item为1，2，3，4，。。。。。这样通过数学知识可以轻松得到我们需要指定(i/2)+1<br>然后lines[i]和lines[i+1]就很清楚了,一条历史占两行</p>
</blockquote>
<p>然后一开始我是生成存到另一个txt文本再粘贴到html，后来还是接口方便,可以直接读取上传的历史的txt就可以自动生成html<br>现在工作室需要的小的接口，我都写到了homepageserver里面，这个也一样,生成json接口,前端js处理一下以后就可以只更新txt文本自动生成时间线了</p>
<blockquote>
<p>还有就是我在浏览的时候,从上到下，很难看到最新的历史,我就想倒序展示时间线，把最新的历史放到首处<br>查了好多,最后得出感觉最优雅的倒序读取文本方式 file.readlines()[::-1]<br>orzzzzzzz,原谅我一开始没想到这种方式////////////</p>
</blockquote>
<p>PS：代码已经在github了</p>
<h2 id="PPT批量上传的脚本"><a href="#PPT批量上传的脚本" class="headerlink" title="PPT批量上传的脚本"></a>PPT批量上传的脚本</h2><blockquote>
<p>ppt上传处是form表单提交，但是又和平常遇到的python表单不同,是属于multipart/form-data<br>是将文件以二进制的形式上传，这样可以实现多种类型的文件上传<br>这时候我们会首先想到requests库来模拟浏览器上传文件<br>但是在这里对于这种形式的上传,用requests另一个工具库requests_toolbelt更为方便(这是查了一个下午的结论)<br>专门针对multipart/form-data这种上传</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">title = os.path.splitext(file)[<span class="number">0</span>] <span class="comment">#获取文件名</span></span><br><span class="line">houzhui = os.path.splitext(file)[<span class="number">1</span>] <span class="comment">#获取后缀</span></span><br><span class="line">open_file = basedir + <span class="string">'/'</span> + file_dir + <span class="string">'/'</span> + file</span><br><span class="line">headers = &#123;<span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0'</span>&#125;</span><br><span class="line">multipart_encoder = MultipartEncoder(</span><br><span class="line">fields=&#123;</span><br><span class="line">      <span class="string">'title'</span>: title,</span><br><span class="line">     <span class="string">'cate'</span>: str(cate_id),</span><br><span class="line">     <span class="string">'type'</span>: str(type_id),</span><br><span class="line">     <span class="string">'status'</span>: str(<span class="number">1</span>),</span><br><span class="line">     <span class="string">'file'</span>: (<span class="string">'file'</span> + houzhui, open(open_file, <span class="string">'rb'</span>))&#125;</span><br><span class="line">   )</span><br><span class="line">headers[<span class="string">'Content-Type'</span>] = multipart_encoder.content_type</span><br><span class="line">upload = requests.post(<span class="string">"https://ppt.atcumt.com/file"</span>,</span><br><span class="line">data=multipart_encoder, headers=headers)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这里哦我们需要注意几点：一点是这个库源码会将参数encoder，这时候int是不允许作为参数的,所以有了str(1)<br>更为重要的一点,官方文档指出file的键值是(文件名,open(文件路径,’打开方式’))<br>但是我在实施过程中运行脚本一直提示没有文件,最后经过很漫长很漫长的搜索后,我正在进入流程源码调试<br>邢组发现文件名只能是英文……………然后解决了,用了指定’file’加后缀的形式,反正最后显示的title<br>是参数title的键值</p>
</blockquote>
<p>对于requests,当时也使用的中文，一直失败，用了这个他的另一个拓展库，如果requests用下面的方式<br>也是英文文件名字的话不知道会不会成功？我没有再去实践…<br>(不过requests似乎已经说明对于这种multipart上传用requests_toolbelt更合适)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">data = &#123;</span><br><span class="line">    <span class="comment"># 标题等参数</span></span><br><span class="line">    &#125;</span><br><span class="line">file = &#123;</span><br><span class="line">    filename : open(open_file,<span class="string">'rb'</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 然后requests.post</span></span><br></pre></td></tr></table></figure>
<p>PS：实际上整个文章技术含量不高,恩,我可能是想表达,以后要想起来利用技术简化一些事情</p>

      
    </div>

    
      
      



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/multipart-form-data/">multipart/form-data</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/2018/05/11/百度AI人脸识别体验/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">百度AI人脸识别体验</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/2018/04/04/如何接入易班第三方登录/">
        <span class="next-text nav-default">如何接入易班第三方登录</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div> 
    </div>
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="https://github.com/bay1" class="iconfont icon-github" title="github"></a>
        
      
    
      
        
          <a href="http://weibo.com/3190704711/profile?topnav=1&wvr=6&is_all=1" class="iconfont icon-weibo" title="weibo"></a>
        
      
    
      
    
      
    
      
    
    
    
  </div>


<div class="copyright">
  <span class="copyright-year">
    
    &copy; 
     
      2016 - 
    
    2018
    <span class="author">bay1</span>
  </span>
</div>
      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://bay1.top/2018/05/11/模拟上传multipart-form-data-脚本生成时间轴/';
        this.page.identifier = '2018/05/11/模拟上传multipart-form-data-脚本生成时间轴/';
        this.page.title = '模拟上传multipart/form-data/脚本生成时间轴';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//https-blog-flywinky-top-1.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>



    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>
<script src="/js/prettify.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('pre').addClass('prettyprint');
   prettyPrint();
 })
</script>
  </body>
</html>
